package com.book.management.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.book.business.entity.BookIndent;
import com.book.common.entity.QueryRequest;
import com.book.management.entity.SendBack;
import com.book.management.mapper.SendBackMapper;
import com.book.management.service.SendBackService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.List;

/**
 * @Author: 何胤宏
 * @Description:  20173969
 */

@Service
public class SendBackServiceImpl extends ServiceImpl<SendBackMapper, SendBack> implements SendBackService {

    @Override
    public List<SendBack> listSendBack(SendBack sendBack) {
        return this.baseMapper.selectList(null);
    }

    @Override
    public IPage<SendBack> sendBackPage(QueryRequest request, SendBack sendBack) {
        QueryWrapper<SendBack> sendBackQueryWrapper = new QueryWrapper<>();
        if (sendBack.getBackdate() != null) {
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
            sendBackQueryWrapper.like("backdate",simpleDateFormat.format(sendBack.getBackdate()));
        }
        if (StringUtils.isNotEmpty(sendBack.getBookname())) {
            sendBackQueryWrapper.like("bookname", sendBack.getBookname());
        }
        if (StringUtils.isNotEmpty(sendBack.getPrincipal())) {
            sendBackQueryWrapper.like("principal", sendBack.getPrincipal());
        }
        Page<SendBack> sendBackPage = new Page<>(request.getPageNum(), request.getPageSize());
        return this.page(sendBackPage, sendBackQueryWrapper);
    }

    @Override
    public void addSendBack(SendBack sendBack) {
        this.save(sendBack);
    }

    @Override
    public void updateSendBack(SendBack sendBack) {
        this.saveOrUpdate(sendBack);
    }

    @Override
    public void deleteSendBack(String[] sendBack) {
        List<String> list = Arrays.asList(sendBack);
        this.removeByIds(list);

    }

    @Override
    public List<SendBack> applySendBack(String[] sendBack) {
        List<String> list = Arrays.asList(sendBack);
        return  (List<SendBack>)listByIds(list);
    }
}
